﻿/*********************************************************************************/
/**    Pattern Preset Color Extractor                                           **/
/**    Copyright (C) 2012  Anja Knackstedt                                      **/
/**                                                                             **/
/**    This program is free software: you can redistribute it and/or modify     **/
/**    it under the terms of the GNU General Public License as published by     **/
/**    the Free Software Foundation, either version 3 of the License, or        **/
/**    (at your option) any later version.                                      **/
/**                                                                             **/
/**    This program is distributed in the hope that it will be useful,          **/
/**    but WITHOUT ANY WARRANTY; without even the implied warranty of           **/
/**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            **/
/**    GNU General Public License for more details.                             **/
/**                                                                             **/
/**    You should have received a copy of the GNU General Public License        **/
/**    along with this program.  If not, see <http://www.gnu.org/licenses/>.    **/
/**                                                                             **/
/*********************************************************************************/

using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

namespace PatternPresetColorExtractor
{
    public partial class PPCEGui : Form
    {

        private string _saveColor0Text = "";
        private string _saveColor1Text = "";
        private string _saveColor2Text = "";
        private string _saveColor3Text = "";
  
        private int _saveCursorPosition0 = 0;
        private int _saveCursorPosition1 = 0;
        private int _saveCursorPosition2 = 0;
        private int _saveCursorPosition3 = 0;

        private Pattern _pattern;
        
        public PPCEGui()
        {
            InitializeComponent();
        }
        public PPCEGui(string filePath): this()
        {
            openSimsPatternFile(filePath);
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception)
            {
               
            }
        }

        private void buttonOpen_Click(object sender, EventArgs e)
        {
            try
            {                
                string filePath = String.Empty;

                if (openPatternDialog.ShowDialog() == DialogResult.OK)
                {
                    filePath = openPatternDialog.FileName;

                    openSimsPatternFile(filePath);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("File must be a pattern file", "Error while opening");
            }
        }

        private void openSimsPatternFile(string filePath)
        {
            _pattern = null;

            FileInfo file = new FileInfo(filePath);
            
            if (file.Extension.ToLower() == ".package")
            {
                PackageReader packageReader = new PackageReader();
                _pattern = packageReader.read(filePath, false);
                _pattern.DisplayPath = filePath;

                XmlDecoder xmlDecoder = new XmlDecoder();
                xmlDecoder.decode(_pattern);
            }
            else if (file.Extension.ToLower() == ".sims3pack")
            {
                Sims3PackXmlReader sims3packReader = new Sims3PackXmlReader();
                PatternPackage patternPackage = sims3packReader.read(filePath);

                XmlDecoder xmlDecoder = new XmlDecoder();
                xmlDecoder.decode(patternPackage);

                PackageFileCreator packageFileCreator = new PackageFileCreator();
                packageFileCreator.createTmpPackageFile(patternPackage);

                PackageReader packageReader = new PackageReader();
                _pattern = packageReader.read(patternPackage.TmpPackagePath, true);
                _pattern.DisplayPath = patternPackage.Sims3PackPath;

                xmlDecoder.decode(_pattern);
            }

            displayPattern(_pattern);
        }

        private void displayPattern(Pattern pattern)
        {
            resetGui();
            
            if (pattern != null)
            {
                if (pattern.Colors[0] != Color.Empty)
                {
                    textBoxColor0.Text = ColorTranslator.ToHtml(pattern.Colors[0]).Remove(0,1);
                    _saveColor0Text     = textBoxColor0.Text;

                    colorBox0.BackColor = pattern.Colors[0];
                }

                if (pattern.Colors[1] != Color.Empty)
                {
                    textBoxColor1.Text = ColorTranslator.ToHtml(pattern.Colors[1]).Remove(0, 1);
                    _saveColor1Text     = textBoxColor1.Text;

                    colorBox1.BackColor = pattern.Colors[1];
                }

                if (pattern.Colors[2] != Color.Empty)
                {
                    textBoxColor2.Text = ColorTranslator.ToHtml(pattern.Colors[2]).Remove(0, 1);
                    _saveColor2Text     = textBoxColor2.Text;

                    colorBox2.BackColor = pattern.Colors[2];
                }

                if (pattern.Colors[3] != Color.Empty)
                {
                    textBoxColor3.Text = ColorTranslator.ToHtml(pattern.Colors[3]).Remove(0, 1);
                    _saveColor3Text     = textBoxColor3.Text;

                    colorBox3.BackColor = pattern.Colors[3];
                }
            }

            pictureBoxIcon.Image = pattern.Icon;
            textBoxFilePath.Text = pattern.DisplayPath;
            textBoxSurfaceMaterial.Text = pattern.SurfaceMaterial;
            textBoxCategory.Text = pattern.Category;
        }

        private void resetGui()
        {
            textBoxColor0.Text = "";
            textBoxColor1.Text = "";
            textBoxColor2.Text = "";
            textBoxColor3.Text = "";

            textBoxCategory.Text = "";
            textBoxSurfaceMaterial.Text = "";

            textBoxFilePath.Text = "";

            _saveColor0Text = "";
            _saveColor1Text = "";
            _saveColor2Text = "";
            _saveColor3Text = "";

            _saveCursorPosition0 = 0;
            _saveCursorPosition1 = 0;
            _saveCursorPosition2 = 0;
            _saveCursorPosition3 = 0;

            colorBox0.BackColor = SystemColors.Control;
            colorBox1.BackColor = SystemColors.Control;
            colorBox2.BackColor = SystemColors.Control;
            colorBox3.BackColor = SystemColors.Control;
        }

        private void textBoxColor0_Click(object sender, EventArgs e)
        {
            if (textBoxColor0.Text != "")
            {
                Clipboard.SetText(textBoxColor0.Text);

                _saveColor0Text         = textBoxColor0.Text;
                _saveCursorPosition0    = textBoxColor0.SelectionStart;

                setCopiedTextToTextbox(textBoxColor0);

                timerLabelC0.Enabled = true;
                timerLabelC0.Start();
            }
        }

        private void textBoxColor1_Click(object sender, EventArgs e)
        {
            if (textBoxColor1.Text != "")
            {
                Clipboard.SetText(textBoxColor1.Text);

                _saveColor1Text         = textBoxColor1.Text;
                _saveCursorPosition1    = textBoxColor1.SelectionStart;

                setCopiedTextToTextbox(textBoxColor1);

                timerLabelC1.Enabled = true;
                timerLabelC1.Start();
            }
        }

        private void textBoxColor2_Click(object sender, EventArgs e)
        {
            if (textBoxColor2.Text != "")
            {
                Clipboard.SetText(textBoxColor2.Text);

                _saveColor2Text         = textBoxColor2.Text;
                _saveCursorPosition2    = textBoxColor2.SelectionStart;

                setCopiedTextToTextbox(textBoxColor2);

                timerLabelC2.Enabled = true;
                timerLabelC2.Start();
            }
        }

        private void textBoxColor3_Click(object sender, EventArgs e)
        {
            if (textBoxColor3.Text != "")
            {
                
                Clipboard.SetText(textBoxColor3.Text);

                _saveColor3Text         = textBoxColor3.Text;
                _saveCursorPosition3    = textBoxColor3.SelectionStart;

                setCopiedTextToTextbox(textBoxColor3);

                timerLabelC3.Enabled = true;
                timerLabelC3.Start();
            }
        }

        private void setCopiedTextToTextbox(TextBox textbox)
        {
            textbox.BackColor   = Color.SteelBlue;
            textbox.ForeColor   = Color.White;
            textbox.Text        = "  Copied";
        }

        private void markTextInTextbox(TextBox textbox)
        {
            textbox.SelectionStart = 0;
            textbox.SelectionLength = textbox.Text.Length;
        }
        private void resetTextboxColor(TextBox textbox)
        {
            textbox.BackColor = SystemColors.Window;
            textbox.ForeColor = SystemColors.WindowText;
        }

        private void timerLabelC0_Tick(object sender, EventArgs e)
        {
            timerLabelC0.Stop();
            timerLabelC0.Enabled = false;

            textBoxColor0.Text              = _saveColor0Text;
            textBoxColor0.SelectionStart    = _saveCursorPosition0;

            resetTextboxColor(textBoxColor0);          
        }

        private void timerLabelC1_Tick(object sender, EventArgs e)
        {
            timerLabelC1.Stop();
            timerLabelC1.Enabled = false;

            textBoxColor1.Text              = _saveColor1Text;
            textBoxColor1.SelectionStart    = _saveCursorPosition1;

            resetTextboxColor(textBoxColor1);            
        }

        private void timerLabelC2_Tick(object sender, EventArgs e)
        {
            timerLabelC2.Stop();
            timerLabelC2.Enabled = false;

            textBoxColor2.Text              = _saveColor2Text;
            textBoxColor2.SelectionStart    = _saveCursorPosition2;

            resetTextboxColor(textBoxColor2);           
        }

        private void timerLabelC3_Tick(object sender, EventArgs e)
        {
            timerLabelC3.Stop();
            timerLabelC3.Enabled = false;

            textBoxColor3.Text              = _saveColor3Text;
            textBoxColor3.SelectionStart    = _saveCursorPosition3;

            resetTextboxColor(textBoxColor3);
        }

        private void PPCEGui_DragEnter(object sender, DragEventArgs e)
        {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop, false);

            if (files.Length > 1)
            {
                e.Effect = DragDropEffects.None;
            }
            else if(files.Length == 1)
            {
                FileInfo file = new FileInfo(files[0]);

                if (file.Extension.ToLower() == ".package" || file.Extension.ToLower() == ".sims3pack")
                {
                    e.Effect = DragDropEffects.All;
                }
                else
                {
                    e.Effect = DragDropEffects.None;
                }
            }
           
        }

        private void PPCEGui_DragDrop(object sender, DragEventArgs e)
        {
            try
            {
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop, false);
                openSimsPatternFile(files[0]);
            }
            catch (Exception)
            {
                MessageBox.Show("File must be a pattern file", "Error while opening");
            }
        }

        private void exportPngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_pattern != null)
            {
                saveIconDialog.FileName = _pattern.Title;

                if (saveIconDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    //export icon
                    pictureBoxIcon.Image.Save(saveIconDialog.FileName);
                }
            }
            
        }
    }
}
